﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWorkBlogDAL
{
    // Ansvar: Martin
   public partial class BlogPost
    {
       // Create
       public static int InsertNewBlogPost(BlogPost blogPost)
       {
           BlogDB.BlogPosts.Add(blogPost);
           int numberOfAffectedRows = BlogDB.Save();
           return numberOfAffectedRows;
       }
       
       // Read (get blogposts as list)
       public static List<BlogPost> GetBlogPostsToList()
       {
           var query = BlogDB.BlogPosts
               .Where(b => b.IsDelete == false)
               .Select(b => b).ToList();
           return query;
       }
       // Read (get blogpostobject)
       public static List<BlogPost>  GetBlogPostlist(int blogId)
       {
           var query = BlogDB.BlogPosts
               .Where(b => b.BlogId == blogId && b.IsDelete == false).ToList();
               
           return query;
       }
       public static BlogPost GetBlogPostPostWithPostId(int postId)
       {
           var query = BlogDB.BlogPosts
               .FirstOrDefault(b => b.BlogPostId == postId && b.IsDelete == false);

           return query;
       }
       // Update
       public static int UpdateBlogPost(BlogPost blogPost)
       {
           var query = BlogDB.BlogPosts
               .FirstOrDefault(n => n.BlogPostId == blogPost.BlogPostId);
           if (query != null)
           {
               query.PostContent = blogPost.PostContent;
               query.PostTitle = blogPost.PostTitle;

               query.LastModifyDate = DateTime.Now.Date;
               BlogDB.BlogPosts.AddOrUpdate(query);
               return BlogDB.Save();
           }
           return 0;
       }
       // Delete
       public static int DeleteBlogPost(BlogPost blogPost)
       {
           var query = BlogDB.BlogPosts
               .FirstOrDefault((b => b.BlogPostId == blogPost.BlogPostId));
           if (query != null)
           {
               query.IsDelete = true;

               BlogDB.BlogPosts.AddOrUpdate(query);
           }
           return BlogDB.Save();
       }
       public static int DeleteBlogPostWithId(int postId)
       {
           var query = BlogDB.BlogPosts
               .FirstOrDefault((b => b.BlogPostId == postId));
           if (query != null)
           {
               query.IsDelete = true;

               BlogDB.BlogPosts.AddOrUpdate(query);
           }
           return BlogDB.Save();
       }
       
       // konstruktor som tar alla inparametrar
       public BlogPost(int blogPostId,string postContent,DateTime postDate, int blogId,DateTime lastModifyDate,bool isDelete)
       {
           BlogPostId = blogPostId;
           PostContent = postContent;
           PostDate = postDate;
           BlogId = blogId;
           LastModifyDate = lastModifyDate;
           IsDelete = isDelete;
       }

        //  Konstruktor som tar in alla parametrar utom LastModifyDate (för att den är nullable)
       public BlogPost(int blogPostId, string postContent, DateTime postDate, int blogId, bool isDelete)
       {
           BlogPostId = blogPostId;
           PostContent = postContent;
           PostDate = postDate;
           BlogId = blogId;
           IsDelete = isDelete;
       }

       // ToString som kollar om LastModifyDate är null eller inte
       public override string ToString()
       {
           if (LastModifyDate == null)
           {
               return string.Format("BlogPostID: {0}\r\n" +
                                    "PostDate: {1}\r\n" +
                                    "PostContent: {2}\r\n", BlogPostId, PostDate, PostContent);
           }
           else
           {
               return string.Format("BlogPostID: {0}\r\n" +
                                    "PostDate: {1}\r\n" +
                                    "Last time modified: {2}\r\n"+
                                    "PostContent: {3}\r\n"
                                    , BlogPostId, PostDate,LastModifyDate, PostContent);
           }

           }
       }
    }


